package com.serkol.ams.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serkol.ams.bean.MyPage;
import com.serkol.ams.bean.MyPageOld;
import com.serkol.ams.entity.LoginLog;
import com.serkol.ams.mapper.LoginLogMapper;
import com.serkol.ams.modal.SqlFeildModal;
import com.serkol.ams.service.LoginLogService;
import com.serkol.handler.MyLambdaQueryWrapper;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @author pingan
* @description 针对表【login_log】的数据库操作Service实现
* @createDate 2022-09-16 19:47:38
*/
@Service
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper, LoginLog>
    implements LoginLogService{


  //  @Cacheable(cacheNames = "loginlog", key = "#token")
    @Override
    public LoginLog isLogin(String token) {
        QueryWrapper<LoginLog> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(LoginLog::getToken,token).orderByDesc(LoginLog::getId).eq(LoginLog::getState,0);


        queryWrapper.last("limit 1");
        LoginLog one = this.getOne(queryWrapper);
        if(one!=null){
            return  one;
        }else{
            return null;
        }

    }

    @Override
    public MyPage<LoginLog> findByList(int pageSize, int current) {
        // 输入参数验证
        if (pageSize <= 0 || current <= 0) {
            throw new IllegalArgumentException("Invalid page size or current page number");
        }

        try {
            MyLambdaQueryWrapper<LoginLog> queryWrapper = new MyLambdaQueryWrapper<>();
            queryWrapper.authName(LoginLog::getUserId).as("userName")
                    .select(LoginLog::getId, LoginLog::getIp, LoginLog::getTerminal, LoginLog::getUserId,
                            LoginLog::getParentUserId, LoginLog::getAgentId, LoginLog::getState,
                            LoginLog::getUserName, LoginLog::getToken, LoginLog::getMyEvnVersion,
                            LoginLog::getMyVersion, LoginLog::getMyLanguage, LoginLog::getLocation,
                            LoginLog::getCreateDate, LoginLog::getUpdateDate);
            queryWrapper.orderByDesc(LoginLog::getId);

            Page<LoginLog> page = new Page<>(current, pageSize);
            IPage<LoginLog> loginLogPage = this.page(page, queryWrapper);

            if (loginLogPage == null || loginLogPage.getRecords().isEmpty()) {
                return MyPage.emptyPage();
            }

            return MyPage.parsePage(loginLogPage);
        } catch (Exception e) {
            // 异常处理
           // logger.error("Error occurred while fetching login logs", e);
            throw new RuntimeException("Failed to fetch login logs", e);
        }
    }

    @Override
    public List<Map<String, Object>> loginChartStastic() {
        Date date = new Date();
   /*     MyLambdaQueryWrapper<UsersWalletLog> queryWrapper = new MyLambdaQueryWrapper<>();
//dateFormat(UsersWalletLog::getCreateDate,DateFormat.DATE_MONTH).as("dateName")
        queryWrapper.sum(UsersWalletLog::getPrice).as("price").dateFormat(UsersWalletLog::getCreateDate, DateFormat.DATE_MONTH).as("name").notIn(UsersWalletLog::getStatus, 2).
                eq(UsersWalletLog::getState, 1);
        queryWrapper.last(" GROUP BY name ORDER BY price DESC");*/
        QueryWrapper<LoginLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(SqlFeildModal.getLoginLogStatastica());
        queryWrapper.groupBy("ymd").orderByAsc("name");
       // queryWrapper.groupBy("name").orderByDesc("name");
        List<Map<String, Object>> maps = this.listMaps(queryWrapper);
        return maps;
    }

    @Override
    public boolean outLogin(Long userId) {
        LambdaUpdateWrapper<LoginLog> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(LoginLog::getState, 1);
        lambdaUpdateWrapper.eq(LoginLog::getUserId, userId);
        return this.update(lambdaUpdateWrapper);
    }
}




